package main;
import java.util.ArrayList;

public abstract class Subject {
	protected int minsize = 0; //min observers num
	protected ArrayList<Observer> observers;
	protected String name;

	public Subject(String cn) {
		observers = new ArrayList<>();
		name = cn;
	}

	public void attach(Observer o) {
		observers.add(o);
	}

	public void detach(Observer o) {
		if (observers.size() > minsize)
			observers.remove(o);
		else
			throw new MinimalSizeException(this.name+" should have at least "+minsize+" observers");
	}

	public void notifyAddedEvents(Event e) {
		for (Observer o: observers)
			o.updateAdded(this, e);
	}

	public void notifyRemovedEvents(Event e) {
		for (Observer o: observers)
			o.updateRemoved(this, e);
	}
}
